import store from '../store/index.js'

// 根据返回权限创建 用户对应的异步路由
export const dynamicRouter = [
  {
    path: '/A',
    name: 'A',
    component: () => import('@/views/A/A.vue'),
    meta: {
      requiresRole: ['admin'],
      title: '标签A',
      hidden: false
    },
    children: [
      {
        name: 'A1',
        path: 'A1',
        component: () => import('@/views/A/A1.vue'),
        meta: {
          requiresRole: ['admin'],
          title: '选项A1',
          hidden: true
        }
      },
      {
        name: 'A2',
        path: 'A2',
        component: () => import('@/views/A/A2.vue'),
        meta: {
          requiresRole: ['admin'],
          title: '选项A2',
          hidden: true
        }
      }
    ]
  },
  {
    path: '/B',
    name: 'B',
    component: () => import('@/views/B/B.vue'),
    meta: {
      requiresRole: ['user'],
      title: '标签B',
      hidden: true
    },
    children: [
      {
        name: 'B1',
        path: 'B1',
        component: () => import('@/views/B/B1.vue'),
        meta: {
          requiresRole: ['user'],
          title: '选项B1',
          hidden: true
        }
      },
      {
        name: 'B2',
        path: 'B2',
        component: () => import('@/views/B/B2.vue'),
        meta: {
          requiresRole: ['user'],
          title: '选项B2',
          hidden: true
        }
      }
    ]
  }
]

// 每次路由跳转都会申请权限信息 重复添加路由会有警告
async function createRouter() {
  try {
    // 获取用户权限
    await store.dispatch('userAuth/getAuthMenus')

    // 得到最终的路由信息
    let finallyRoutes = filterRouter(store.state.userAuth.authMenus, dynamicRouter)

    // 保存到vuex用于el-menu的渲染
    store.commit('userAuth/SET_USER_FINALLY_MENUS', finallyRoutes)

    return finallyRoutes
  } catch (error) {
    console.log(error)
  }
}

const initiDynamicRouter = 1

export default initiDynamicRouter
